﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MovieCollectionManager.DataModel;

namespace MovieCollectionManager
{
    public partial class CreateLoanForm : Form
    {
        public CreateLoanForm()
        {
            InitializeComponent();
        }

        private void CreateLoanForm_Load(object sender, EventArgs e)
        {
            BindMovies();
            listBoxMoviesToLend.DisplayMember = "Title";
            dateTimePickerReturnDate.Value = dateTimePickerReturnDate.Value.AddDays(7);
        }

        private void BindMovies()
        {
            comboBoxMovies.DataSource = null;
            comboBoxMovies.Items.Clear();

            using (var db = new MovieCollectionConteiner())
            {
                var moviesResult = (from movie in db.Movies
                                    where movie.Lent == false
                                    select movie);

                if (moviesResult.Count() == 0)
                    return;

                comboBoxMovies.DataSource = moviesResult.ToList();
                comboBoxMovies.DisplayMember = "Title";
                comboBoxMovies.ValueMember = "Id";
            }
        }

        private void buttonAddMovie_Click(object sender, EventArgs e)
        {
            if (listBoxMoviesToLend.Items.Contains(comboBoxMovies.SelectedItem))
                return;

            listBoxMoviesToLend.Items.Add(comboBoxMovies.SelectedItem);
        }

        private void buttonRemoveMovie_Click(object sender, EventArgs e)
        {
            if (listBoxMoviesToLend.SelectedItem != null)
                listBoxMoviesToLend.Items.Remove(listBoxMoviesToLend.SelectedItem);
        }

        private void buttonLend_Click(object sender, EventArgs e)
        {
            if (textBoxLendTo.Text == "")
            {
                MessageBox.Show("Informe o nome do destinatário.", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if (textBoxPhone.Text == "")
            {
                MessageBox.Show("Informe o telefone do destinatário.", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if (listBoxMoviesToLend.Items.Count == 0)
            {
                MessageBox.Show("Insira pelo menos um filme para emprestar.", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            using (var db = new MovieCollectionConteiner())
            {
                foreach (Movie movie in listBoxMoviesToLend.Items)
                {
                    Loan newLoan = new Loan();
                    newLoan.Owner = LoginForm.loggedUser;
                    newLoan.Receiver = textBoxLendTo.Text;
                    newLoan.ReceiverPhone = Convert.ToInt32(textBoxPhone.Text);
                    newLoan.LoanDate = dateTimePickerDate.Value;
                    newLoan.ReturnDate = dateTimePickerReturnDate.Value;
                    newLoan.MovieId = movie.Id;

                    db.Loans.AddObject(newLoan);
                }
                db.SaveChanges();
            }

            UpdateMoviesLentState();
            ClearFields();
            BindMovies();
            MessageBox.Show("Empréstimo criado!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void ClearFields()
        {
            textBoxLendTo.Text = "";
            textBoxPhone.Text = "";
            listBoxMoviesToLend.Items.Clear();
        }

        private void UpdateMoviesLentState()
        {
            using (var db = new MovieCollectionConteiner())
            {
                foreach (Movie movie in listBoxMoviesToLend.Items)
                {
                    Movie movieToUpdate = (from movie1 in db.Movies
                                           where movie1.Id == movie.Id
                                           select movie1).First();
                    movieToUpdate.Lent = true;
                }
                db.SaveChanges();
            }
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
